using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Light.Assets.Scripts.EventManager;
using UnityEngine;
using UnityEngine.UI;

namespace Light
{
    public class MInMapController : MonoBehaviour
    {
        //��Ҫ���ֵĵ���
        private readonly List<(Transform, RectTransform)> enemyTransformToObjectDic = new();
        private readonly Vector2 sizeData = new(20, 20);
        private Sprite enemyPointSprite;

        private GameObject mapUi;

        //��������Ĺ���vector2
        private Vector2 originVector2 = Vector2.zero;

        private Transform player;

        //׼��ʵ����������
        private Sprite playerPointSprite;

        private GameObject playerPointUI;

        //��Ҫ���ٵ�PlayerTransform
        private Transform playerTrasform;

        private Vector2 targetVector2 = Vector2.one; //����v

        private async void Awake()
        {
            mapUi = transform.Find("Map").gameObject;

            playerPointUI = transform.Find("PlayerPoint").gameObject;

            EventManager.Register<PlayerRegisterEvent>(OnPlayerRegisterEventHandle);
            EventManager.Register<EnemyRegisterEvnet>(OnEnemyRegisterEventHandle);
            EventManager.Register<EnemyDeleteEvent>(OnEnemyDeleteEvnetHandle);

            //����ͼ��
            ResManager.Instance.LoadAsync<Sprite>("RedPoint", s => { enemyPointSprite = s; });
            ResManager.Instance.LoadAsync<Sprite>("BluePoint", s => { playerPointSprite = s; }, LoadType.AddressAbles);

            await UniTask.WaitUntil(() => playerPointSprite != null);
            await UniTask.WaitUntil(() => enemyPointSprite != null);

            playerPointUI.GetComponent<Image>().sprite = playerPointSprite;
        }

        private void Start()
        {
        }

        private void Update()
        {
            PlayerPointMove();

            EnemyPointsMove();
        }

        /// <summary>
        ///     ������Ƶ���ǵ㽲���ƶ�����
        /// </summary>
        private void EnemyPointsMove()
        {
            foreach (var item in enemyTransformToObjectDic)
            {
                var dis = Vector3.Distance(playerTrasform.position, item.Item1.localPosition);
                if (dis > 50)
                    item.Item2.gameObject.SetActive(false);
                else
                    item.Item2.gameObject.SetActive(true);

                originVector2.x = item.Item1.position.x - playerTrasform.localPosition.x;
                originVector2.y = item.Item1.position.y - playerTrasform.localPosition.y;

                RatioCalculation();

                item.Item2.anchoredPosition = targetVector2;
            }
        }

        /// <summary>
        ///     �ƶ�mapͼ��
        /// </summary>
        private void PlayerPointMove()
        {
            if (playerTrasform == null) return;
            originVector2 = playerTrasform.position;
            originVector2.y = playerTrasform.position.z;

            RatioCalculation();

            //mapUi.transform.anchoredPosition = Vector2.zero - targetVector2;
        }

        /// <summary>
        ///     �������㣬Ŀǰʹ�õı�����1 ��2��Ҫ���ͼ������ʾ�뾶50�Ĺ���
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void RatioCalculation()
        {
            targetVector2 = originVector2 * 2;
        }

        /// <summary>
        ///     ��ÿһ���ɹ�ע��ĵ��˽������ֺͱ���
        /// </summary>
        /// <param name="event"></param>
        private void OnEnemyRegisterEventHandle(EnemyRegisterEvnet @event)
        {
            var image = Instantiate(playerPointUI, mapUi.transform).GetComponent<Image>();
            image.sprite = enemyPointSprite;
            image.rectTransform.sizeDelta = sizeData;
            image.gameObject.SetActive(false);
            enemyTransformToObjectDic.Add((@event.transform, image.rectTransform));
        }

        /// <summary>
        ///     ��������ʧ��ɾ��
        /// </summary>
        /// <param name="event"></param>
        private void OnEnemyDeleteEvnetHandle(EnemyDeleteEvent @event)
        {
            var data = enemyTransformToObjectDic.Find(item => item.Item1 == @event.transform);
            enemyTransformToObjectDic.Remove(data);
            if (data.Item2.gameObject != null)
                Destroy(data.Item2.gameObject);
        }

        private void OnPlayerRegisterEventHandle(PlayerRegisterEvent @event)
        {
            playerTrasform = @event.transform;
        }
    }
}